using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace MicroMsg.UI.UserContrl
{
	public class TLDetailCmtsItem : UserControl
	{
		private const string TAG = "TLDetailCmtsItem";
		private const double cDCmtAreaWidt = 315.0;
		private const double cDNickAndTimeMargin = 20.0;
		private const int MAX_ELEMENT_NUM = 400;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(SnsComment), typeof(TLDetailCmtsItem), new PropertyMetadata(null, new PropertyChangedCallback(TLDetailCmtsItem.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		public MMStandardUIEvent mClickDlg;
		private string mStrContent;
		private NickNameButton mBtnNickTo = new NickNameButton();
		private Grid mGridBtnToContainer;
		private TextBlock mTxtWhoToWho = new TextBlock();
		private Grid mGridTxtWhoToWho;
		private string lastContentCache;
		private string mStrLastNames;
		private static List<TLDetailCmtsItem> sListItems = new List<TLDetailCmtsItem>();
		internal Button btnRoot;
		internal UserHeadCtrl imgCtrlHead;
		internal NickNameButton nickCtrl;
		internal TextBlock txtTime;
		internal Paragraph sParagraph;
		internal Button btnDel;
		internal Grid txtFromLayout;
		internal TextBlock txtFrom;
		internal Canvas canvasCmtsLine;
		private bool _contentLoaded;
		public SnsComment ItemObject
		{
			get
			{
				return (SnsComment)base.GetValue(TLDetailCmtsItem.ItemObjectProperty);
			}
			set
			{
				base.SetValue(TLDetailCmtsItem.ItemObjectProperty, value);
			}
		}
		public TLDetailCmtsItem()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == TLDetailCmtsItem.ItemObjectProperty)
			{
				(dependencyObject_0 as TLDetailCmtsItem).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		private void btnRoot_Click(object sender, RoutedEventArgs e)
		{
			if (this.mClickDlg != null)
			{
				this.mClickDlg(SnsCmtsOpKind.Kind_ClickLayout, this.ItemObject);
			}
		}
		private void btnDel_Click(object sender, RoutedEventArgs e)
		{
			if (this.mClickDlg != null)
			{
				this.mClickDlg(SnsCmtsOpKind.Kind_Del, this.ItemObject);
			}
		}
		private void initBtnContainer()
		{
			this.nickCtrl.SetMode(NickNameButtonMode.WithColon);
			this.nickCtrl.txtNickName.FontSize = 20.0;
			this.nickCtrl.txtTag.set_FontSize(20.0);
		}
		private void initBtnToContainer()
		{
			if (this.mGridBtnToContainer != null)
			{
				return;
			}
			this.mGridBtnToContainer = new Grid();
			this.mGridBtnToContainer.set_VerticalAlignment(2);
			this.mBtnNickTo.SetMode(NickNameButtonMode.WithColon);
			this.mBtnNickTo.txtNickName.FontSize = 20.0;
			this.mBtnNickTo.txtTag.set_FontSize(20.0);
			this.mBtnNickTo.set_VerticalAlignment(2);
			this.mBtnNickTo.set_Margin(new Thickness(2.0, 0.0, 3.0, -5.0));
		}
		private InlineUIContainer getNickTo(string strUserName)
		{
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			this.initBtnToContainer();
			if (this.mBtnNickTo.get_Parent() != null)
			{
				InlineUIContainer inlineUIContainer2 = this.mBtnNickTo.get_Parent() as InlineUIContainer;
				if (inlineUIContainer2 != null)
				{
					inlineUIContainer2.set_Child(null);
				}
			}
			this.mBtnNickTo.ItemObject = strUserName;
			inlineUIContainer.set_Child(this.mBtnNickTo);
			return inlineUIContainer;
		}
		private void initTxtWhoToWhoContainer()
		{
			if (this.mGridTxtWhoToWho != null)
			{
				return;
			}
			this.mGridTxtWhoToWho = new Grid();
			this.mTxtWhoToWho.set_FontSize(20.0);
			this.mGridTxtWhoToWho.set_VerticalAlignment(2);
			this.mGridTxtWhoToWho.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
			this.mTxtWhoToWho.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
			this.mTxtWhoToWho.set_VerticalAlignment(2);
			this.mGridTxtWhoToWho.get_Children().Add(this.mTxtWhoToWho);
			this.mTxtWhoToWho.set_Foreground(MMTheme.GetInstance().Sns_Brush_CmtContent);
		}
		private Inline getTxtWhoToWho()
		{
			Run run = new Run();
			run.set_Text(strings.WC_COMMENT_TO_TA);
			return run;
		}
		private void RefreshItem()
		{
			SnsComment itemObject = this.ItemObject;
			if (itemObject == null)
			{
				return;
			}
			this.initBtnContainer();
			if (itemObject.strUserName == AccountMgr.strUsrName)
			{
				this.btnDel.set_Visibility(0);
			}
			else
			{
				this.btnDel.set_Visibility(1);
			}
			if (this.isContact(itemObject))
			{
				this.txtFromLayout.set_Visibility(1);
				this.txtFrom.set_Text("");
			}
			else
			{
				this.txtFromLayout.set_Visibility(0);
				this.txtFrom.set_Text(this.getSourceFrom(itemObject.nSource));
			}
			this.imgCtrlHead.ItemObject = itemObject.strUserName;
			this.mStrContent = itemObject.strContent;
			this.txtTime.set_Text(Util.formatTimeInChatMsg((long)((ulong)itemObject.nCreateTime)));
			this.txtTime.InvalidateMeasure();
			this.nickCtrl.txtNickName.set_MaxWidth(295.0 - this.txtTime.get_ActualWidth());
			this.nickCtrl.ItemObject = itemObject.strUserName;
			this.RefreshRichText();
		}
		private bool isContact(SnsComment snsComment_0)
		{
			Contact contact = StorageMgr.contact.method_0(snsComment_0.strUserName);
			return contact != null && ContactMgr.isContact(contact);
		}
		private string getSourceFrom(uint sourceType)
		{
			string text = "";
			switch (sourceType)
			{
			case 18u:
				text = strings.WC_COMMENT_LIST_FROM_LBS;
				break;
			case 22u:
			case 23u:
			case 24u:
			case 26u:
			case 27u:
			case 28u:
			case 29u:
				text = strings.WC_COMMENT_LIST_FROM_SHAKE;
				break;
			case 25u:
				text = strings.WC_COMMENT_LIST_FROM_BOTTLE;
				break;
			case 30u:
				text = strings.WC_COMMENT_LIST_FROM_QRCODE;
				break;
			}
			if (string.IsNullOrEmpty(text))
			{
				text = strings.WC_COMMENT_LIST_FROM_GENERAL;
			}
			return text;
		}
		private void RefreshRichText()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mStrContent))
			{
				return;
			}
			string text = this.ItemObject.strUserName;
			if (!string.IsNullOrEmpty(this.ItemObject.strReplyUsername))
			{
				text = text + "@" + this.ItemObject.strReplyUsername;
			}
			if (this.mStrContent == this.lastContentCache && text == this.mStrLastNames)
			{
				return;
			}
			this.mStrLastNames = text;
			this.lastContentCache = this.mStrContent;
			this.sParagraph.get_Inlines().Clear();
			List<InLineResult> inLineResult = RichTextHelper.GetInLineResult(this.mStrContent, (int)this.sParagraph.get_FontSize(), null, null);
			if (!string.IsNullOrEmpty(this.ItemObject.strReplyUsername))
			{
				this.sParagraph.get_Inlines().Add(this.getTxtWhoToWho());
				this.sParagraph.get_Inlines().Add(this.getNickTo(this.ItemObject.strReplyUsername));
			}
			int num = 0;
			using (List<InLineResult>.Enumerator enumerator = inLineResult.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					InLineResult current = enumerator.get_Current();
					if (current.mInline != null)
					{
						num++;
						this.sParagraph.get_Inlines().Add(current.mInline);
						if (num >= 400)
						{
							Run run = new Run();
							run.set_Text("......");
							this.sParagraph.get_Inlines().Add(run);
							break;
						}
					}
				}
			}
		}
		public static TLDetailCmtsItem GetOneItem()
		{
			if (TLDetailCmtsItem.sListItems != null && TLDetailCmtsItem.sListItems.get_Count() != 0)
			{
				TLDetailCmtsItem result = Enumerable.First<TLDetailCmtsItem>(TLDetailCmtsItem.sListItems);
				TLDetailCmtsItem.sListItems.RemoveAt(0);
				return result;
			}
			return new TLDetailCmtsItem();
		}
		public static void PushOneItem(TLDetailCmtsItem item)
		{
			if (item == null)
			{
				return;
			}
			if (TLDetailCmtsItem.sListItems == null)
			{
				TLDetailCmtsItem.sListItems = new List<TLDetailCmtsItem>();
			}
			if (item.get_Parent() != null)
			{
				return;
			}
			TLDetailCmtsItem.sListItems.Add(item);
		}
		private void btnNick_Click(object sender, RoutedEventArgs e)
		{
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/TLDetailCmtsItem.xaml", 2));
			this.btnRoot = (Button)base.FindName("btnRoot");
			this.imgCtrlHead = (UserHeadCtrl)base.FindName("imgCtrlHead");
			this.nickCtrl = (NickNameButton)base.FindName("nickCtrl");
			this.txtTime = (TextBlock)base.FindName("txtTime");
			this.sParagraph = (Paragraph)base.FindName("sParagraph");
			this.btnDel = (Button)base.FindName("btnDel");
			this.txtFromLayout = (Grid)base.FindName("txtFromLayout");
			this.txtFrom = (TextBlock)base.FindName("txtFrom");
			this.canvasCmtsLine = (Canvas)base.FindName("canvasCmtsLine");
		}
	}
}
